﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TjZheng.Windows.Forms;
using TjZheng.Framework;
using TjZheng.JiaLi.Process;
using System.Xml;

namespace TjZheng.JiaLi.Winform
{
    public partial class ActivatedForm : TjZhengForm
    {
        public ActivatedForm()
        {
            InitializeComponent();
        }

        private void Btn_Input_Click(object sender, EventArgs e)
        {
            if (this.Txt_Account.Text.IsNull())
            {
                base.ShowMessage("请输入您的帐号");
                return;
            }
            Txt_Input.Text = TicketInfo.CreateInputTicket(this.Txt_Account.Text);
        }

        private void Btn_Trial_Click(object sender, EventArgs e)
        {
            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "Active.xml";
            XmlDocument objXmlDocument = xmlPath.CreateXmlDocument();
            if (objXmlDocument == null)
            {
                base.ShowMessage("对不起，您的试用文件丢失");
                return;
            }
            XmlNode objInputXmlNode = objXmlDocument.DocumentElement.SelectSingleNode("//Trial/Input");
            if (objInputXmlNode == null)
            {
                base.ShowMessage("对不起，您的Input试用码丢失");
                return;
            }
            XmlNode objValidXmlNode = objXmlDocument.DocumentElement.SelectSingleNode("//Trial/Valid");
            if (objValidXmlNode == null)
            {
                base.ShowMessage("对不起，您的Input试用码丢失");
                return;
            }
            XmlNode objIsActiveXmlNode = objXmlDocument.DocumentElement.SelectSingleNode("//IsActive");
            if (objIsActiveXmlNode == null)
            {
                base.ShowMessage("对不起，您的试用节点丢失");
                return;
            }
            if (objInputXmlNode.InnerText.IsNull() && objValidXmlNode.InnerText.IsNull())
            {
                objInputXmlNode.InnerText = TicketInfo.CreateInputTicket(null);
                objValidXmlNode.InnerText = TicketInfo.CreateValidTicket(objInputXmlNode.InnerText, TimeSpan.FromMinutes(5));
                objIsActiveXmlNode.InnerText = "False";
                objXmlDocument.Save(xmlPath);
                this.DialogResult = DialogResult.OK;
                return;
            }
            if (objInputXmlNode.InnerText.IsNoNull() && objValidXmlNode.InnerText.IsNoNull())
            {
                InvokeResult objInvokeResult = TicketInfo.CheckTrialTicket(objInputXmlNode.InnerText, objValidXmlNode.InnerText);
                if (objInvokeResult.ResultCode == "Success")
                {
                    this.DialogResult = DialogResult.OK;
                    return;
                }
                base.ShowMessage(objInvokeResult.ResultMessage);
                return;
            }
            base.ShowMessage("对不起，请不要篡改密钥");
        }

        private void Btn_Active_Click(object sender, EventArgs e)
        {
            if (this.Txt_Input.Text.IsNull())
            {
                base.ShowMessage("请您先生成机器码");
                return;
            }
            if (this.Txt_Valid.Text.IsNull())
            {
                base.ShowMessage("激活码不能为空");
                return;
            }
            if (this.Txt_Input.Text == this.Txt_Valid.Text)
            {
                base.ShowMessage("机器码不能作为激活码使用");
                return;
            }
            InvokeResult objInvokeResult = TicketInfo.CheckActivatedTicket(this.Txt_Input.Text, this.Txt_Valid.Text);
            if (objInvokeResult.ResultCode != "Success")
            {
                base.ShowMessage(objInvokeResult.ResultMessage);
                return;
            }
            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "Active.xml";
            XmlDocument objXmlDocument = xmlPath.CreateXmlDocument();
            if (objXmlDocument == null)
            {
                base.ShowMessage("对不起，您的激活文件丢失");
                return;
            }
            XmlNode objInputXmlNode = objXmlDocument.DocumentElement.SelectSingleNode("//Active/Input");
            if (objInputXmlNode == null)
            {
                base.ShowMessage("对不起，您的Input激活码丢失");
                return;
            }
            XmlNode objValidXmlNode = objXmlDocument.DocumentElement.SelectSingleNode("//Active/Valid");
            if (objValidXmlNode == null)
            {
                base.ShowMessage("对不起，您的Input激活码丢失");
                return;
            }
            XmlNode objIsActiveXmlNode = objXmlDocument.DocumentElement.SelectSingleNode("//IsActive");
            if (objIsActiveXmlNode == null)
            {
                base.ShowMessage("对不起，您的激活节点丢失");
                return;
            }
            if (objInputXmlNode.InnerText.IsNull() && objValidXmlNode.InnerText.IsNull())
            {
                objInputXmlNode.InnerText = this.Txt_Input.Text;
                objValidXmlNode.InnerText = this.Txt_Valid.Text;
                objIsActiveXmlNode.InnerText = "True";
                objXmlDocument.Save(xmlPath);
            }
            else
            {
                base.ShowMessage("对不起，请不要篡改密钥");
                return;
            }
            base.ShowMessage("恭喜你，激活成功");
            this.DialogResult = DialogResult.OK;
        }

        private void Btn_Close_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
        }
    }
}
